package demo1;

import java.lang.reflect.Array;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86185
 * Date: 2023-04-02
 * Time: 9:37
 */
class MyArray1 {
    public Object[] objects = new Object[10];
    public Object getPos(int pos) {
        return objects[pos];
    }
    public void setVal(int pos, Object val) {
        objects[pos] = val;
    }
}
class MyArray <T>{//<T>是占位符，代表当前类是泛型类
    public MyArray() {

    }
    public MyArray(Class<T> tClass, int capacity) {
        objects = (T[]) Array.newInstance(tClass,capacity);
    }

    public T[] objects;
    //擦除机制 将所有的T替换成Object
    //public Object[] objects = new Object[10];
    public T getPos(int pos) {
        return objects[pos];
    }
    public void setObjects(int pos, T val) {
        objects[pos] = val;
    }
    public Object[] getObjects() {
        return objects;
    }
}
class MyArray2 <T>{
    private Object[] objects = new Object[10];

    public T getPos(int pos) {
        return (T)objects[pos];//强转
    }
    public void setVal(int pos, T val) {
        objects[pos] = val;
    }
    public Object[] getArray() {
        return objects;
    }
}
public class Test {
    public static void main(String[] args) {
        MyArray2<Integer> myArray = new MyArray2<>();
        myArray.setVal(0,1);
        myArray.setVal(1,123);
        Object[] myArray1 = myArray.getArray();
        System.out.println(myArray1[1]);
        System.out.println("====");
        //int val = myArray.getPos(0);

    }
    public static void main3(String[] args) {
        MyArray<Integer> myArray = new MyArray<>();
        myArray.setObjects(0,48);
    }
    public static void main2(String[] args) {
        MyArray<Integer> myArray = new MyArray<>();
        myArray.setObjects(0,1);
        myArray.setObjects(1,123);
        int val = myArray.getPos(0);
        MyArray<String> myArray1 = new MyArray<>();
        myArray1.setObjects(0,"hello");
        myArray1.setObjects(1,"bit");

        String ret = myArray1.getPos(1);
        System.out.println(ret);
        //1.在编译的时候帮我们进行类型的检查
        //2.在编译的时候帮我们进行类型的转换
        //运行的时候没有泛型的概念，jvm中没有泛型，泛型只存在于编译的时期
    }
    public static void main1(String[] args) {
        MyArray myArray = new MyArray();
        myArray.setObjects(0,1);
        myArray.setObjects(1,"123");

        int val = (int)myArray.getPos(0);
        String str = (String) myArray.getPos(1);
        System.out.println(val + " " + str);
    }
}
